/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.xtd.model.inventory;

import com.xtd.model.employment.Employee;
import com.xtd.model.retail.OrderCommodity;
import com.xtd.util.CommodityPropertyEnum;
import java.io.Serializable;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Set;
import javax.persistence.Basic;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import org.hibernate.annotations.IndexColumn;

/**
 *
 * @author Lu Yuanbin
 */
@Entity
@Table(name = "commodity")
@NamedQueries({
    @NamedQuery(name = "Commodity.findByProperty", query = "SELECT c FROM Commodity c WHERE c.property = :property")})
public class Commodity implements Serializable {

    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Basic(optional = false)
    @Column(name = "id")
    private Integer id;
    @Column(name = "name")
    private String name;
    @Column(name = "number")
    private String number;
    @Column(name = "stocks")
    private Integer stocks;
    @Column(name = "unit")
    private String unit;
    @Column(name = "standard")
    private String standard;
    @Column(name = "type")
    private String type;
    @Column(name = "uitPrice")
    private BigDecimal uitPrice;
    @Column(name = "unitCost")
    private BigDecimal unitCost;
    @Column(name = "discountable")
    private boolean discountable;
    @Column(name = "active")
    private boolean active;
    @Column(name = "property")
    @Enumerated(EnumType.STRING)
    private CommodityPropertyEnum property;
    @Column(name = "amount")
    private Integer amount;
    @Column(name = "createdTime")
    @Temporal(TemporalType.TIMESTAMP)
    private Date createdTime;
    @OneToMany(cascade = CascadeType.ALL, mappedBy = "commodity", fetch = FetchType.EAGER)
    @org.hibernate.annotations.Cascade({org.hibernate.annotations.CascadeType.DELETE, org.hibernate.annotations.CascadeType.DELETE_ORPHAN})
    private List<CommodityHasCommodity> subCommodityCollection;
    @OneToMany(mappedBy = "commodity", fetch = FetchType.EAGER)
    private Set<OrderCommodity> orderCommodityCollection;
    @OneToMany(mappedBy = "commodity", fetch = FetchType.EAGER)
    private Set<PurchaseRecord> purchaseRecordCollection;
    @JoinColumn(name = "createdBy", referencedColumnName = "id")
    @ManyToOne
    private Employee createdBy;
    @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
    @org.hibernate.annotations.Cascade({org.hibernate.annotations.CascadeType.DELETE, org.hibernate.annotations.CascadeType.DELETE_ORPHAN})
    @JoinColumn(name = "commodity")
    @IndexColumn(name = "royalty_position", base = 0)
    private List<CommodityRoyalty> commodityRoyaltyCollection;

    public Commodity() {
    }

    public Commodity(CommodityPropertyEnum property) {
        this.property = property;
        this.createdTime = new Date();
    }

    public Commodity(Integer id) {
        this.id = id;
    }

    public boolean isNew() {
        if (id == null) {
            return true;
        } else {
            return false;
        }
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getNumber() {
        return number;
    }

    public void setNumber(String number) {
        this.number = number;
    }

    public Integer getStocks() {
        return stocks;
    }

    public void setStocks(Integer stocks) {
        this.stocks = stocks;
    }

    public String getUnit() {
        return unit;
    }

    public void setUnit(String unit) {
        this.unit = unit;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public BigDecimal getUitPrice() {
        return uitPrice;
    }

    public void setUitPrice(BigDecimal uitPrice) {
        this.uitPrice = uitPrice;
    }

    public BigDecimal getUnitCost() {
        return unitCost;
    }

    public void setUnitCost(BigDecimal unitCost) {
        this.unitCost = unitCost;
    }

    public boolean getDiscountable() {
        return discountable;
    }

    public void setDiscountable(boolean discountable) {
        this.discountable = discountable;
    }

    public boolean getActive() {
        return active;
    }

    public void setActive(boolean active) {
        this.active = active;
    }

    public CommodityPropertyEnum getProperty() {
        return property;
    }

    public void setProperty(CommodityPropertyEnum property) {
        this.property = property;
    }

    public Set<OrderCommodity> getOrderCommodityCollection() {
        return orderCommodityCollection;
    }

    public void setOrderCommodityCollection(Set<OrderCommodity> orderCommodityCollection) {
        this.orderCommodityCollection = orderCommodityCollection;
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (id != null ? id.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof Commodity)) {
            return false;
        }
        Commodity other = (Commodity) object;
        if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id)) || (this.id == null && other == null && this != other)) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return number + " " + name;
    }

    /**
     * @return the standard
     */
    public String getStandard() {
        return standard;
    }

    /**
     * @param standard the standard to set
     */
    public void setStandard(String standard) {
        this.standard = standard;
    }

    /**
     * @return the subCommodityCollection
     */
    public List<CommodityHasCommodity> getSubCommodityCollection() {

        return subCommodityCollection;
    }

    /**
     * @param subCommodityCollection the subCommodityCollection to set
     */
    public void setSubCommodityCollection(List<CommodityHasCommodity> subCommodityCollection) {
        this.subCommodityCollection = subCommodityCollection;
    }

    public boolean isSuperCommdity() {
        if (subCommodityCollection == null || subCommodityCollection.isEmpty()) {
            return false;
        } else {
            return true;
        }
    }

    /**
     * @return the amount
     */
    public Integer getAmount() {
        return amount;
    }

    /**
     * @param amount the amount to set
     */
    public void setAmount(Integer amount) {
        this.amount = amount;
    }

    public boolean isPackage() {
        return property.equals(CommodityPropertyEnum.套系商品);
    }

    public boolean isStock() {
        return property.equals(CommodityPropertyEnum.材料商品) || property.equals(CommodityPropertyEnum.零售商品);
    }

    /**
     * @return the createdBy
     */
    public Employee getCreatedBy() {
        return createdBy;
    }

    /**
     * @param createdBy the createdBy to set
     */
    public void setCreatedBy(Employee createdBy) {
        this.createdBy = createdBy;
    }

    /**
     * @return the createdTime
     */
    public Date getCreatedTime() {
        return createdTime;
    }

    /**
     * @param createdTime the createdTime to set
     */
    public void setCreatedTime(Date createdTime) {
        this.createdTime = createdTime;
    }

    /**
     * @return the commodityRoyaltyCollection
     */
    public List<CommodityRoyalty> getCommodityRoyaltyCollection() {
        if (commodityRoyaltyCollection == null) {
            commodityRoyaltyCollection = new ArrayList();
        }
        return commodityRoyaltyCollection;
    }

    /**
     * @param commodityRoyaltyCollection the commodityRoyaltyCollection to set
     */
    public void setCommodityRoyaltyCollection(List<CommodityRoyalty> commodityRoyaltyCollection) {
        this.commodityRoyaltyCollection = commodityRoyaltyCollection;
    }

    /**
     * @return the quantity_required
     */
    public Integer getQuantityRequired() {
        int quantityRequired = 0;
        if (isStock()) {
            for (OrderCommodity oc : orderCommodityCollection) {
                quantityRequired = quantityRequired + oc.getQuantity() - oc.getReturnedQuantity() - oc.getStockout();
            }
        }
        return quantityRequired;
    }

    /**
     * @return the purchaseRecordCollection
     */
    public Set<PurchaseRecord> getPurchaseRecordCollection() {
        return purchaseRecordCollection;
    }

    /**
     * @param purchaseRecordCollection the purchaseRecordCollection to set
     */
    public void setPurchaseRecordCollection(Set<PurchaseRecord> purchaseRecordCollection) {
        this.purchaseRecordCollection = purchaseRecordCollection;
    }
}
